package cn.itcast.p2p20.fragments;


import android.support.v4.app.Fragment;
import android.view.View;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.itcast.p2p20.R;
import cn.itcast.p2p20.bean.AssetsData;
import cn.itcast.p2p20.bean.request.QueryRequest;
import cn.itcast.p2p20.bean.response.AssetsResponse;
import cn.itcast.p2p20.net.BaseRequest;
import cn.itcast.p2p20.net.SuccessResultCallback;
import cn.itcast.p2p20.utils.Util;

/**
 * A simple {@link Fragment} subclass.
 */
public class AsstesFragment extends BaseFragment implements SuccessResultCallback<AssetsResponse> {

    @InjectView(R.id.piechart)
    PieChart piechart;


    @Override
    protected void initData() {

        sendRequest(new QueryRequest(mContext, "assets"), AssetsResponse.class, this);

    }

    @Override
    protected void initView(View childFragmentView) {

        ButterKnife.inject(this, childFragmentView);
    }

    @Override
    protected String getTitle() {
        return null;
    }

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_asstes;
    }

    @Override
    public void onSuccessResult(BaseRequest request, AssetsResponse response) {

        AssetsData assetsData = response.data;
        List<String> xVals = new ArrayList<>();
        List<Entry> yVals = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        //设置标签
        PieDataSet dataSet = new PieDataSet(yVals, "资产分布");

        List<AssetsData.AssetsBean> assets = assetsData.getAssets();
        for (int i = 0; i < assets.size(); i++) {
            AssetsData.AssetsBean assetsBean = assets.get(i);
            xVals.add(assetsBean.getTag());
            yVals.add(new Entry(Float.parseFloat(assetsBean.getValue()), i));
            colors.add(Util.generaRandomColor(mContext));
        }


        dataSet.setColors(colors);
        piechart.animateXY(2000, 1000);
        //给饼状图设置数据
        piechart.setData(new PieData(xVals, dataSet));
    }
}
